var roomnumber = document.getElementsByClassName('roomnumber')[0];
var phone = document.getElementsByClassName('phone')[0];
var button = document.getElementsByClassName('button1')[0];
var wrong1 = document.getElementsByClassName('wrong1')[0];
var wrong2 = document.getElementsByClassName('wrong2')[0];
var wrong3 = document.getElementsByClassName('wrong3')[0];


//onsubmit 事件
function submitTest(){
	var check_room = room();
	// console.log(room())
	var check_address = putin();
	if(check_room && check_address){
		return true;
	}
	else{
		return false;
	}
}

//验证寝室号
function room(){
	if(roomnumber.value.length == 6){
		wrong1.style.display = 'none';
		return havey();
	}
	else if(roomnumber.value.length == 5){
		wrong1.style.display = 'none';
		return havey();
	}
	else{
		wrong1.style.display = 'block';
		return false;
	}
}

//验证开头是y
function havey(){
	var str = roomnumber.value.substr(0,1);
	var patt1 = new RegExp("y","i");
	if(isNaN(str)){
		if(patt1.test(str)){
			wrong3.style.display = 'none';
			return true;
		}
		else{
			wrong3.style.display = 'block';
			return false;
		}
	}
	else{
		return true;
	}
}

//判定是否输入联系方式
function putin(){
	if(phone.value.length == 0){
		return true;
	}
	else{
		return address();
	}
}

function address(){
	if(isNaN(phone.value)){
		return email();
	}
	else{
		return tel();
	}
}

function email(){
	//对电子邮件的验证
	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	if(!myreg.text(phone.value)){
		wrong2.style.display = "block";
		myreg.focus();
		return false;
	}
	else{
		wrong2.style.display = "none";
		return true;
	}
}

function tel(){
	if(phone.value.length != 11){
		wrong2.style.display = "block";
		return false;
	}
	else{
		wrong2.style.display = "none";
		return true;
	}
}